import { ref } from 'vue'
import { adapter, request } from '@/request'
import { activityByGet } from '@/core/api/activity'
import type { Activity } from '@/core/interfaces/activity'
import type { RequestWrapper } from '@/core/interfaces/common'

interface Params {
  activityId: number
}

export interface FreeWordsRules {
  key?: string
  activityId: number
  highPrice: number
  id: number
  lowPrice: number
  updateTime: string
  updateUserId: number
  combinedWordNum: number
  wordNum: number
  pcImgNum: number
  ydImgNum: number
}

export interface ActivityFreeWords extends Activity {
  freeWordRules: FreeWordsRules[]
}

export default function getActivity() {
  const service = adapter<RequestWrapper<ActivityFreeWords>>(request)

  const loading = ref(false)
  const response = ref<Partial<ActivityFreeWords>>({})

  const fetch = (params: Params) => {
    const { url, method } = activityByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
